﻿#Region "Copyright"
'————————————————————————————————————————
'	创建人员(Creator)    ：Huangyd
'	创建日期(Date)       ：2012-07-14
'   联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
'   描    述(Description)：
'
'	Copyright (C) 2012 ajayumi 版权所有
'————————————————————————————————————————
#End Region
Imports System.Drawing.Drawing2D
Imports System.Drawing

Namespace Drawing.Form

    Public Class GraphicsPath

#Region "CreateRoundRectanglePath"
        ''' <summary>
        ''' 建立带有圆角样式的路径，把矩形宽减 1,以便画出边框
        ''' </summary>
        ''' <param name="rect">用来建立路径的矩形</param>
        ''' <param name="radius">圆角半径</param>
        ''' <param name="style">圆角的样式</param>
        ''' <returns>建立的路径</returns>
        Public Shared Function CreateRoundRectanglePath(ByVal rect As Rectangle, ByVal radius As Integer, ByVal style As CornerStyle) As System.Drawing.Drawing2D.GraphicsPath
            Return CreateRoundRectanglePath(rect, radius, style, True)
        End Function


        ''' <summary>
        ''' 建立带有圆角样式的路径
        ''' </summary>
        ''' <param name="rect">用来建立路径的矩形</param>
        ''' <param name="radius">圆角半径</param>
        ''' <param name="style">圆角的样式</param>
        ''' <param name="correction">是否把矩形长宽减 1,以便画出边框</param>
        ''' <returns>建立的路径</returns>
        Public Shared Function CreateRoundRectanglePath(ByVal rect As Rectangle, ByVal radius As Integer, ByVal style As CornerStyle, ByVal correction As Boolean) As System.Drawing.Drawing2D.GraphicsPath
            Dim _GraphicsPath As New System.Drawing.Drawing2D.GraphicsPath()
            Dim _RadiusCorrection As Integer = CInt(IIf(correction, 1, 0))

            With _GraphicsPath
                Select Case style
                    Case CornerStyle.None
                        .AddRectangle(rect)
                        Exit Select
                    Case CornerStyle.All
                        '左上角
                        .AddArc(New Rectangle(rect.Left, rect.Top, radius, radius), 180, 90)
                        '上
                        .AddLine(rect.Left + radius, rect.Top, rect.Right - radius, rect.Top)
                        '右上角
                        .AddArc(New Rectangle(rect.Right - radius - _RadiusCorrection, rect.Top, radius, radius), -90, 90)
                        '右
                        .AddLine(rect.Right, rect.Top + radius, rect.Right, rect.Bottom - radius)
                        '右下角
                        .AddArc(New Rectangle(rect.Right - radius - _RadiusCorrection, rect.Bottom - radius - _RadiusCorrection, radius, radius), 0, 90)
                        '下
                        .AddLine(rect.Left + radius, rect.Bottom, rect.Right - radius, rect.Bottom)
                        '左下角
                        .AddArc(New Rectangle(rect.Left, rect.Bottom - radius - _RadiusCorrection, radius, radius), 90, 90)
                        '左
                        .AddLine(rect.Left, rect.Top + radius, rect.Left, rect.Bottom - radius)
                        Exit Select
                    Case CornerStyle.RightBottom
                        '上
                        .AddLine(rect.Left, rect.Top, rect.Right, rect.Top)
                        '右
                        .AddLine(rect.Right, rect.Top, rect.Right, rect.Bottom - radius)
                        '右下角
                        .AddArc(New Rectangle(rect.Right - radius - _RadiusCorrection, rect.Bottom - radius - _RadiusCorrection, radius, radius), 0, 90)
                        '下
                        .AddLine(rect.Right - radius, rect.Bottom, rect.Left, rect.Bottom)
                        '左
                        .AddLine(rect.Left, rect.Bottom, rect.Left, rect.Top)
                        Exit Select
                    Case CornerStyle.Bottom
                        '上
                        .AddLine(rect.Left, rect.Top, rect.Right, rect.Top)
                        '右
                        .AddLine(rect.Right, rect.Top, rect.Right, rect.Bottom - radius)
                        '右下角
                        .AddArc(New Rectangle(rect.Right - radius - _RadiusCorrection, rect.Bottom - radius - _RadiusCorrection, radius, radius), 0, 90)
                        '下
                        .AddLine(rect.Left + radius, rect.Bottom, rect.Right - radius, rect.Bottom)
                        '左下角
                        .AddArc(New Rectangle(rect.Left, rect.Bottom - radius - _RadiusCorrection, radius, radius), 90, 90)
                        '左
                        .AddLine(rect.Left, rect.Top, rect.Left, rect.Bottom - radius)
                        Exit Select
                    Case CornerStyle.Top
                        '左上角
                        .AddArc(New Rectangle(rect.Left, rect.Top, radius, radius), 180, 90)
                        '上
                        .AddLine(rect.Left + radius, rect.Top, rect.Right - radius, rect.Top)
                        '右上角
                        .AddArc(New Rectangle(rect.Right - radius - _RadiusCorrection, rect.Top, radius, radius), -90, 90)
                        '右
                        .AddLine(rect.Right, rect.Top + radius, rect.Right, rect.Bottom)
                        '下
                        .AddLine(rect.Right, rect.Bottom, rect.Left, rect.Bottom)
                        '左
                        .AddLine(rect.Left, rect.Bottom, rect.Left, rect.Top + radius)
                        Exit Select
                    Case CornerStyle.LeftBottom
                        '上
                        .AddLine(rect.Left, rect.Top, rect.Right, rect.Top)
                        '右
                        .AddLine(rect.Right, rect.Top, rect.Right, rect.Bottom)
                        '下
                        .AddLine(rect.Right, rect.Bottom, rect.Left + radius, rect.Bottom)
                        '左下角
                        .AddArc(New Rectangle(rect.Left, rect.Bottom - radius - _RadiusCorrection, radius, radius), 90, 90)
                        '左
                        .AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Top)
                        Exit Select
                    Case Else
                        Throw New NotImplementedException()
                End Select

                .CloseFigure()
            End With

            Return _GraphicsPath
        End Function


#End Region

#Region "CreateFlatRectanglePath"

        ''' <summary>
        ''' 建立带有平角样式的路径
        ''' </summary>
        ''' <param name="rect">用来建立路径的矩形</param>
        ''' <param name="diagonal">对角线距离</param>
        ''' <param name="style">边角样式</param>
        ''' <returns>建立的路径</returns>
        Public Shared Function CreateFlatRectanglePath(ByVal rect As Rectangle, ByVal diagonal As Integer, ByVal style As CornerStyle) As System.Drawing.Drawing2D.GraphicsPath
            Return CreateFlatRectanglePath(rect, diagonal, style, True)
        End Function

        ''' <summary>
        ''' 建立带有平角样式的路径
        ''' </summary>
        ''' <param name="rect">用来建立路径的矩形</param>
        ''' <param name="diagonal">对角线距离</param>
        ''' <param name="style">边角样式</param>
        ''' <param name="correction">是否把矩形长宽减 1,以便画出边框</param>
        ''' <returns>建立的路径</returns>
        Public Shared Function CreateFlatRectanglePath(ByVal rect As Rectangle, ByVal diagonal As Integer, ByVal style As CornerStyle, ByVal correction As Boolean) As System.Drawing.Drawing2D.GraphicsPath
            Dim _GraphicsPath As New System.Drawing.Drawing2D.GraphicsPath()
            Dim _Correction As Integer = CInt(IIf(correction, 1, 0))

            With _GraphicsPath
                Select Case style
                    Case CornerStyle.None
                        .AddRectangle(rect)
                        Exit Select
                    Case CornerStyle.All
                        '左上角
                        .AddLine(rect.Left, rect.Top + diagonal, rect.Left + diagonal, rect.Top)
                        '右上角
                        .AddLine(rect.Right - diagonal, rect.Top, rect.Right, rect.Top + diagonal)
                        '右下角
                        .AddLine(rect.Right, rect.Bottom - diagonal - _Correction, rect.Right - diagonal - _Correction, rect.Bottom)
                        '左下角
                        .AddLine(rect.Left + diagonal + _Correction, rect.Bottom, rect.Left, rect.Bottom - diagonal - _Correction)
                        Exit Select
                    Case CornerStyle.Top
                        '左上角
                        .AddLine(rect.Left, rect.Top + diagonal, rect.Left + diagonal, rect.Top)
                        '右上角
                        .AddLine(rect.Right - diagonal, rect.Top, rect.Right, rect.Top + diagonal)
                        .AddLine(rect.Right, rect.Top + diagonal, rect.Right, rect.Bottom)
                        .AddLine(rect.Right, rect.Bottom, rect.Left, rect.Bottom)
                        Exit Select
                    Case CornerStyle.RightBottom
                        '上
                        .AddLine(rect.Left, rect.Top, rect.Right, rect.Top)
                        '右
                        .AddLine(rect.Right, rect.Top, rect.Right, rect.Top - diagonal)
                        '右下角
                        .AddLine(rect.Right, rect.Bottom - diagonal - _Correction, rect.Right - diagonal - _Correction, rect.Bottom)
                        '下
                        .AddLine(rect.Right - diagonal, rect.Bottom, rect.Left, rect.Bottom)
                        Exit Select
                    Case CornerStyle.LeftBottom
                        '上
                        .AddLine(rect.Left, rect.Top, rect.Right, rect.Top)
                        '右
                        .AddLine(rect.Right, rect.Top, rect.Right, rect.Top)
                        '下
                        .AddLine(rect.Right, rect.Bottom, rect.Left + diagonal, rect.Bottom)
                        '左下角
                        .AddLine(rect.Left + diagonal + _Correction, rect.Bottom, rect.Left, rect.Bottom - diagonal - _Correction)
                        Exit Select
                    Case Else
                        Throw New NotImplementedException()
                End Select

                .CloseFigure()
            End With

            Return _GraphicsPath
        End Function
#End Region

#Region "CreateMaximizePath"

        ''' <summary>
        ''' 创建最大化的路径
        ''' </summary>
        ''' <param name="rect"></param>
        ''' <param name="thickness"></param>
        ''' <param name="isMaximize"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CreateMaximizePath(ByVal rect As Rectangle, ByVal thickness As Integer, ByVal isMaximize As Boolean) As System.Drawing.Drawing2D.GraphicsPath
            Return CreateMaximizePath(rect, thickness, isMaximize, 5)
        End Function


        ''' <summary>
        ''' 创建最大化的路径
        ''' </summary>
        ''' <param name="rect"></param>
        ''' <param name="thickness">厚度</param>
        ''' <param name="space">间隔</param>
        ''' <param name="isMaximize"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CreateMaximizePath(ByVal rect As Rectangle, ByVal thickness As Integer, ByVal isMaximize As Boolean, ByVal space As Integer) As System.Drawing.Drawing2D.GraphicsPath
            Dim _SquareRect As Rectangle = GraphicsHelper.GetCenterRectangle(rect)
            _SquareRect.Inflate(-space, -space)

            Dim _GraphicsPath As New System.Drawing.Drawing2D.GraphicsPath()

            With _GraphicsPath
                If isMaximize = False Then
                    .AddRectangle(_SquareRect)
                    _SquareRect.Inflate(-thickness, -thickness)
                    .AddRectangle(_SquareRect)
                Else
                    Dim _Offset As Integer = If(thickness <= 2, thickness, 2)
                    Dim _Rectangle As Rectangle = New Rectangle(_SquareRect.Left, _SquareRect.Top + _Offset, _SquareRect.Width - _Offset, _SquareRect.Height - _Offset)
                    .AddRectangle(_Rectangle)
                    _Rectangle.Inflate(-thickness, -thickness)
                    .AddRectangle(_Rectangle)

                    .AddLines(New Point() { _
                                New Point(_SquareRect.Left + _Offset, _SquareRect.Top), _
                                New Point(_SquareRect.Right, _SquareRect.Top), _
                                New Point(_SquareRect.Right, _SquareRect.Bottom - _Offset), _
                                New Point(_SquareRect.Right - _Offset, _SquareRect.Bottom - _Offset), _
                                New Point(_SquareRect.Right - _Offset, _SquareRect.Top + _Offset), _
                                New Point(_SquareRect.Left + _Offset, _SquareRect.Top + _Offset)})
                End If

                .CloseFigure()
            End With

            Return _GraphicsPath
        End Function
#End Region

#Region "CreateMinimizePath"

        ''' <summary>
        ''' 创建最小化按钮路径
        ''' </summary>
        ''' <param name="rect"></param>
        ''' <param name="thickness"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CreateMinimizePath(ByVal rect As Rectangle, ByVal thickness As Integer) As System.Drawing.Drawing2D.GraphicsPath
            Return CreateMinimizePath(rect, thickness, 5)
        End Function

        ''' <summary>
        ''' 创建最小化按钮路径
        ''' </summary>
        ''' <param name="rect"></param>
        ''' <param name="thickness"></param>
        ''' <param name="space"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CreateMinimizePath(ByVal rect As Rectangle, ByVal thickness As Integer, ByVal space As Integer) As System.Drawing.Drawing2D.GraphicsPath
            Dim _SquareRect As Rectangle = GraphicsHelper.GetCenterRectangle(rect)
            _SquareRect.Inflate(-space, -space)

            Dim _GraphicsPath As New System.Drawing.Drawing2D.GraphicsPath()

            With _GraphicsPath
                .AddRectangle(New Rectangle(_SquareRect.Left, _SquareRect.Bottom - thickness, _SquareRect.Width, thickness))
                .CloseFigure()
            End With

            Return _GraphicsPath
        End Function

#End Region

#Region "CreateClosePath"
        ''' <summary>
        ''' 创建关闭按钮路径
        ''' </summary>
        ''' <param name="rect"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function CreateClosePath(ByVal rect As Rectangle, ByVal thickness As Integer, ByVal space As Integer) As System.Drawing.Drawing2D.GraphicsPath
            Dim _SquareRect As Rectangle = GraphicsHelper.GetCenterRectangle(rect)
            _SquareRect.Inflate(-1, -space)
            Dim _CenterPoint As New Point(_SquareRect.Left + CInt(_SquareRect.Width / 2), _SquareRect.Top + CInt(_SquareRect.Height / 2))

            Dim _GraphicsPath As New System.Drawing.Drawing2D.GraphicsPath()

            With _GraphicsPath
                .AddLines(New Point() { _
                          New Point(_SquareRect.Left, _SquareRect.Top), _
                          New Point(_SquareRect.Left + thickness, _SquareRect.Top), _
                          New Point(_CenterPoint.X, _CenterPoint.Y - CInt(thickness / 2)), _
                          New Point(_SquareRect.Right - thickness, _SquareRect.Top), _
                          New Point(_SquareRect.Right, _SquareRect.Top), _
                          New Point(_CenterPoint.X + CInt(thickness / 2), _CenterPoint.Y), _
                          New Point(_SquareRect.Right, _SquareRect.Bottom), _
                          New Point(_SquareRect.Right - thickness, _SquareRect.Bottom), _
                          New Point(_CenterPoint.X, _CenterPoint.Y + CInt(thickness / 2)), _
                          New Point(_SquareRect.Left + thickness, _SquareRect.Bottom), _
                          New Point(_SquareRect.Left, _SquareRect.Bottom), _
                          New Point(_CenterPoint.X - CInt(thickness / 2), _CenterPoint.Y)})
                '.AddLines(New Point() { _
                '         New Point(_SquareRect.Left, _SquareRect.Top), _
                '         New Point(_SquareRect.Left + thickness, _SquareRect.Top), _
                '         New Point(_SquareRect.Right, _SquareRect.Bottom), _
                '         New Point(_SquareRect.Right - thickness, _SquareRect.Bottom), _
                '         New Point(_SquareRect.Left, _SquareRect.Top)})
                '.AddLines(New Point() { _
                '         New Point(_SquareRect.Right - thickness, _SquareRect.Top), _
                '         New Point(_SquareRect.Right, _SquareRect.Top), _
                '         New Point(_SquareRect.Left + thickness, _SquareRect.Bottom), _
                '         New Point(_SquareRect.Left, _SquareRect.Bottom), _
                '         New Point(_SquareRect.Right - thickness, _SquareRect.Top)})

                .CloseFigure()
            End With

            Return _GraphicsPath
        End Function

#End Region

    End Class
End Namespace


